﻿using System;
using System.Data;
using Atm.DAL;
using HocLapTrinhWeb.DAL;

namespace Atm.BLL
{
    public class v_AtmBLL : t_AtmBLL
    {
        #region Variable
        ClassBaseDAL _ClassBaseDAL;
        #endregion

        #region Public Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="conn"></param>
        public v_AtmBLL(Connection conn)
            : base(conn)
        {

        }

        public vwAtm.vw_AtmDataTable GetAllAtmForGridView(int startRowIndex, int maximumRows)
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwAtm.vw_AtmDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt)
                    {
                        StartRow = startRowIndex,
                        MaxRows = maximumRows,
                        OrderByClause = dt.AtmIDColumn.ColumnName + " desc"
                    };
                    if (_ClassBaseDAL.FillData(dt))
                        return dt;
                    AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return null;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return null;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return null;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }

        public int GetAllAtmRowCount()
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwAtm.vw_AtmDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt);
                    var rowcount = _ClassBaseDAL.GetRowCount();
                    if (rowcount == -1)
                        AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return rowcount;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return -1;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return -1;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }


        public vwAtm.vw_AtmDataTable GetAllAtmForGridView(int startRowIndex, int maximumRows, int ProvinceID,int AtmTypeID, int IsActive)
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwAtm.vw_AtmDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt)
                    {
                        StartRow = startRowIndex,
                        MaxRows = maximumRows,
                        OrderByClause = dt.CreatedDateColumn.ColumnName + " desc"
                    };
                    _ClassBaseDAL.WhereClause = " 1 = 1 ";
                    if (ProvinceID != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.ProvinceIDColumn.ColumnName + "=@ProvinceID";
                        _ClassBaseDAL.AddParams("@ProvinceID", SqlDbType.Int, ProvinceID, ParameterDirection.Input);
                    }
                    if (AtmTypeID != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.AtmTypeIDColumn.ColumnName + "=@AtmTypeID";
                        _ClassBaseDAL.AddParams("@AtmTypeID", SqlDbType.Int, AtmTypeID, ParameterDirection.Input);
                    }
                    if (IsActive != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.IsActiveColumn.ColumnName + "=@IsActive";
                        _ClassBaseDAL.AddParams("@IsActive", SqlDbType.Int, IsActive, ParameterDirection.Input);
                    }
                    if (_ClassBaseDAL.FillData(dt))
                        return dt;
                    AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return null;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return null;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return null;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }

        public int GetAllAtmRowCount(int ProvinceID,int AtmTypeID, int IsActive)
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwAtm.vw_AtmDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt);
                    _ClassBaseDAL.WhereClause = " 1 = 1 ";
                    if (ProvinceID != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.ProvinceIDColumn.ColumnName + "=@ProvinceID";
                        _ClassBaseDAL.AddParams("@ProvinceID", SqlDbType.Int, ProvinceID, ParameterDirection.Input);
                    }
                    if (AtmTypeID != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.AtmTypeIDColumn.ColumnName + "=@AtmTypeID";
                        _ClassBaseDAL.AddParams("@AtmTypeID", SqlDbType.Int, AtmTypeID, ParameterDirection.Input);
                    }
                    if (IsActive != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.IsActiveColumn.ColumnName + "=@IsActive";
                        _ClassBaseDAL.AddParams("@IsActive", SqlDbType.Int, IsActive, ParameterDirection.Input);
                    }
                    var rowcount = _ClassBaseDAL.GetRowCount();
                    if (rowcount == -1)
                        AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return rowcount;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return -1;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return -1;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }

        public vwAtm.vw_AtmDataTable GetAllAtmForGridView(int startRowIndex, int maximumRows, string txtSearch, int IsActive)
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwAtm.vw_AtmDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt)
                    {
                        StartRow = startRowIndex,
                        MaxRows = maximumRows,
                        OrderByClause = dt.AtmIDColumn.ColumnName + " desc"
                    };
                    _ClassBaseDAL.WhereClause = " 1 = 1 ";
                    if (txtSearch != "")
                    {
                        _ClassBaseDAL.WhereClause += " and (" + dt.AddressColumn.ColumnName + " like N'%" + txtSearch + "%' OR " + dt.AtmNameColumn.ColumnName + " like N'%" + txtSearch + "%' )";
                    }
                    if (IsActive != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.IsActiveColumn.ColumnName + "=@IsActive";
                        _ClassBaseDAL.AddParams("@IsActive", SqlDbType.Int, IsActive, ParameterDirection.Input);
                    }
                    if (_ClassBaseDAL.FillData(dt))
                        return dt;
                    AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return null;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return null;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return null;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }



        #endregion

    }
}
